/**
 * Simple cross-platform mutex library.
 * Adapted from:
 * http://www.codeproject.com/Articles/25569/Cross-Platform-Mutex
 */

#ifndef MUTEX_H
#define MUTEX_H

// Platform-specific headers
#if defined(__unix__)
#include <pthread.h>
#elif defined(_WIN32)
#include <windows.h>
#include <process.h>
#else
#error "Unable to determine platform."
#endif

// Platform-specific data type
#if defined(__unix__)
typedef pthread_mutex_t MUTEX;
#elif defined(_WIN32)
typedef HANDLE MUTEX;
#endif

// Wrapper class
class Mutex
{
public:
  Mutex();
  ~Mutex();

  bool lock();
  bool unlock();
  bool trylock();

private:
  MUTEX _mutex;
};

#endif /* MUTEX_H */
